package util.methods;

import java.nio.channels.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

import java.io.*;

public class FileHelpers 
{
	public static String getFileDate(String fileName) {
		File f = new File(fileName);
	   	if  (!(f.isFile() && f.exists())) return null;
	   	
		long d = f.lastModified();
	   	Date date = new Date(d);
	   	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
	   	return fmt.format(date);
	}
	
	public static String getFileDateTime(String fileName) {
		File f = new File(fileName);
	   	if  (!(f.isFile() && f.exists())) return null;
	   	
		long d = f.lastModified();
	   	Date date = new Date(d);
	   	return date.toString();
	}
	
	public static void copyFile( File in, File out ) throws Exception 
	{
	     FileChannel sourceChannel = new
	          FileInputStream(in).getChannel();
	     FileChannel destinationChannel = new
	          FileOutputStream(out).getChannel();
	     sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);

	     sourceChannel.close();
	     destinationChannel.close();
	}
	
	public static boolean mergeFiles(String file1, String file2, String outfile) 
	{
		try{
			File f1 = new File(file1);
			File f2 = new File(file2);
			File o = new File(outfile);

			OutputStream out = new FileOutputStream(o,true);
			InputStream in = new FileInputStream(f1);
			
			byte[] buf = new byte[8192];
			int len;
			while ((len = in.read(buf)) > 0){
			       out.write(buf, 0, len);
			}
			in.close();
			
			in = new FileInputStream(f2);
			while ((len = in.read(buf)) > 0){
			       out.write(buf, 0, len);
			}
			in.close();			
			out.close();
		}
		catch(FileNotFoundException ex){
			System.err.println(ex.getMessage() + " in the specified directory.");
			return false;
		}
		catch(IOException e){
			 System.err.println(e.getMessage());  
			 return false;
		}
		return true;
	}
	
	public static void deleteFile ( String strPath )
	{
		File theFile = new File ( strPath );
		theFile.delete();
	}
	
	public static boolean existDir(String strPath) {
		if (strPath == null) return false;
	   	File path = new File(strPath);
    	    if ( !(path.exists()) && path.isDirectory() ) return false;
	    return true;
	}
	
	public static boolean deleteDir (String strPath) {
		if (strPath == null) return false;
	   	File path = new File(strPath);
    	    if ( !(path.exists()) && path.isDirectory() ) return false;
	    
    	    List <File> files;
    	    try {
    	    		files = getFileListing(strPath);
    	    }
    	    catch (Exception e) { return false;}
    	    
	    for(int i=0; i<files.size(); i++) {
	    	   File f = files.get(i);
           if(f.isDirectory()) {
             deleteDir(f.getName());
           }
           else {
             f.delete();
           }
	    }
	    path.delete();
	    return true;
	}
	
    public static List<File> getFileListing(String aStartingDir) throws FileNotFoundException 
    {
    		File startingDir = new File(aStartingDir);
    	
		if (startingDir == null)
			throw new IllegalArgumentException("Directory should not be null.");
		
		if (!startingDir.exists()) 
			throw new FileNotFoundException("Directory does not exist: " + startingDir);
		
		if (!startingDir.isDirectory()) 
			throw new IllegalArgumentException("Is not a directory: " + startingDir);
		
		if (!startingDir.canRead()) 
			throw new IllegalArgumentException("Directory cannot be read: " + startingDir);

		List<File> result = new ArrayList<File>();
		
		File[] filesAndDirs = startingDir.listFiles();
		List<File> filesDirs = Arrays.asList(filesAndDirs);
		for(File file : filesDirs) {
			result.add(file); 
		}
		Collections.sort(result);
		return result;
    }
    
    public static boolean fileExists(String filepath)
    {
	    	if (filepath == null) return false;
	    	File f = new File(filepath);
	    	return f.isFile() && f.exists();
    }
    
    public static boolean isAbsolutePath(String filepath)
    {
    	if (filepath == null) return false;
    	File f = new File(filepath);
    	return f.isAbsolute();
    }
    
    public static String addAbsolutePath(String absPath, String filepath)
    {
    	if (filepath == null) return null;
    	File f = new File(filepath);
    	if (f.isAbsolute() || filepath.equals(""))
    		return filepath;
    	
    	return absPath + "/" + filepath;
    }
	
    private FileHelpers () { }; // Don't instantiate
}
